﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Handrise.CDCable.Mobile.Business
{
    public class PlanService
    {
        /// <summary>
        /// 获取登录用户的默认任务计划（状态为未执行和执行中的）
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<Plan> GetDefaultPlans(string userName)
        {
            return AppLogic.dataService.Find<Plan>("State <> '已完成' and UserName='" + userName + "'","StartDateTime");
        }

        /// <summary>
        /// 获取登录用户的所有任务计划
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<Plan> GetAllPlans(string userName)
        {
            return AppLogic.dataService.Find<Plan>("UserName ='" + userName  + "'","");
        }

        /// <summary>
        /// 通过设备信息获取对应的任务计划
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="deviceCode">设备编码</param>
        /// <param name="deviceName">设备名称</param>
        /// <param name="deviceType">设备类型</param>
        /// <returns></returns>
        public List<Plan> GetPlansByDevice(string userName,string deviceCode, string deviceName, string deviceType)
        {
            string filter = "";
            if(deviceCode != "")
                filter += "and (DeviceCode = '" + deviceCode + "') ";
            if(deviceName != "")
                filter += "and (DeviceName like '%" + deviceName + "%') ";
            if(deviceType != "")
                filter += "and (DeviceType = '" + deviceType + "') " ;

            filter = "PlanID in (Select distinct PlanID from PlanDevice Where (UserName = '" + userName + "') " + filter + ")";
            return AppLogic.dataService.Find<Plan>(filter, "StartDateTime");
        }

        /// <summary>
        /// 从服务器获取对应用户的所有任务计划（下载任务）
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public void GetAllPlansFromServer(string userName)
        {
        }

        /// <summary>
        /// 获取计划任务
        /// </summary>
        /// <param name="planID"></param>
        /// <returns></returns>
        public Plan GetPlan(string planID)
        {
            return AppLogic.dataService.GetEntityByKey<Plan>(planID);
        }

        /// <summary>
        /// 执行任务计划
        /// </summary>
        /// <param name="planID"></param>
        public Plan ExcutePlan(string planID)
        {
            Plan plan = GetPlan(planID);
            if (plan != null)
            {
                plan.State = "执行中";
                AppLogic.dataService.Update<Plan>(plan);
            }

            return plan;
        }

        /// <summary>
        /// 完成任务计划
        /// </summary>
        /// <param name="planID"></param>
        public Plan CompletePlan(string planID)
        {
            Plan plan = GetPlan(planID);
            if (plan != null)
            {
                plan.State = "已完成";
                AppLogic.dataService.Update<Plan>(plan);
            }

            return plan;
        }

        /// <summary>
        /// 处理新接收到的计划
        /// </summary>
        /// <param name="plans"></param>
        public void DealNewReceivedPlan(List<object> plans)
        {
            foreach (Plan  plan in plans)
            {
                //判断新计划的状态,如果是取消任务则删除任务
                if (plan.State == "已终止")
                {
                    DeletePlan(plan.PlanID);
                    continue;
                }

                //新计划
                AppLogic.dataService.Add<Plan>(plan);

            }
        }

        /// <summary>
        /// 删除计划
        /// </summary>
        /// <param name="planID"></param>
        /// <returns></returns>
        public void DeletePlan(string planID)
        {
            //删除计划
            AppLogic.dataService.Delete<Plan>(planID);

            //删除计划下的设备
            PlanDeviceService planDeviceService = new PlanDeviceService();
            planDeviceService.DeletePlanDeviceByPlan(planID);

            //删除巡视信息
            PatrolInfoService patrolInfoService = new PatrolInfoService();
            patrolInfoService.DeletePlanInfoByPlan(planID);

        }

        

    }
}
